﻿angular.module('app')

.controller('GtjaCommentController', function($scope,$http,TaskService) {
	
	// edit/update/cancel/delete
	$scope.edit = function(){
		$scope.showTaskEdit = !$scope.showTaskEdit;
		angular.copy($scope.selectedTask, $scope.master);
		console.log("copying selectedTask to master. new master: " + $scope.master);
	}
	$scope.update = function(){
		// save task
		$scope.showTaskEdit = !$scope.showTaskEdit;
	}
	$scope.cancel = function(){
		angular.copy($scope.master, $scope.selectedTask);
		console.log("copying master to selectedTask. new selectedTask: " + $scope.selectedTask);
		
		$scope.setNextSDateTimeTextInput($scope.selectedTask);
		$scope.showTaskEdit = !$scope.showTaskEdit;
	}
	$scope.delete = function(){
		if (!confirm("Do you want to delete it?")) {
			window.event.returnValue = false;
		} else {
			// delete task
		}
	}
	
	
    // used for e/u/d stock
    $scope.showTaskEdit = true;
    $scope.master = {};
	$scope.taskOptions = [
		{
			name: 'YahooCsvDataLoader',
			abbr: 'Yahoo data loader',
			description: 'download daily csv data from yahoo',
			nextSDateTime: new Date(),
			frequency: 4,
			status: 1,
		},
		{
			name: 'LastTransDayUpdater',
			abbr: 'last transaction day updater',
			description: 'update last transaction day to stockprice',
			nextSDateTime: new Date(),
			frequency: 4,
			status: 1,
		},
		{
			name: 'SilverBulletStockRegister',
			abbr: '全能式股票注册',
			description: 'register stock automatically by looping number range',
			nextSDateTime: new Date(),
			frequency: 4,
			status: 1,
		}
	];
	
	$scope.taskOptions.selectedName = $scope.taskOptions[0].name;
	$scope.chooseTaskForModal = function(){
		$('#modal-createtask').modal('hide')
		
		var selectedOption;
		for (var index in $scope.taskOptions) {
			var obj = $scope.taskOptions[index]
			if (obj.name == $scope.taskOptions.selectedName) {
				selectedOption = obj;
				break;
			}
		}
		console.log(selectedOption)
		$scope.tasks.splice(0,0, selectedOption);
		$scope.selectedTask = selectedOption;
		$scope.initSelectedRow();
		$scope.paginationConf.totalItems = $scope.paginationConf.totalItems + 1;
	}
	
	$scope.monitorTaskChg = function(){
		console.log($scope.taskOptions.selectedName)
	}
	
	$scope.selectCurrentTask=function(index) {
    	$scope.selectedTask = $scope.tasks[index];
    	$scope.selectedIndex = index;
    };
    
	//////////////////Initialization /////////////////////
	
	$scope.currentPage = "GtjaMgr";
	
	//////////////////Initialization /////////////////////
	
})
